package com.geekjam.dingdongandroid.api;

import android.accounts.OnAccountsUpdateListener;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * by SEGFault Xiawei
 * Date: 3/9/13 Time: 3:20 PM
 */
public class DingDongMessageProxy extends Proxy {

    private static final String API_UPLOAD_SOUND = "/sound/upload";
    private static final String API_GET_RANDOM_SOUND = "/sound/random";
    public static final String RECORD_FILENAME = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/message.3gp";
    public static final String RECEIVE_FILENAME = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/alarm.3gp";

    public static void uploadSound(final OnUploadComplete onUploadComplete) {
        File myFile = new File(RECORD_FILENAME);
        RequestParams params = new RequestParams();
        try {
            params.put("file", myFile);
        } catch (FileNotFoundException e) {
        }

        Proxy.post(DingDongMessageProxy.API_UPLOAD_SOUND, params, new JsonHttpResponseHandler() {
            public void onSuccess(JSONObject response) {
//                String uid = response.optLong("")
                onUploadComplete.succeeded();
            }

            @Override
            public void onFailure(Throwable e, JSONObject errorResponse) {
                super.onFailure(e, errorResponse);
                onUploadComplete.failed(e,errorResponse);
            }

            @Override
            protected void handleFailureMessage(Throwable e, String responseBody) {
                // TODO - Overridden method to be implemented
                super.handleFailureMessage(e, responseBody);
                onUploadComplete.failed(e,responseBody);
            }
        });
    }

    public static void getRandomSound(final OnGetRandomSoundComplete onGetRandomSoundComplete) {

        Proxy.get(DingDongMessageProxy.API_GET_RANDOM_SOUND, null, new JsonHttpResponseHandler() {
            public void onSuccess(JSONObject response) {
                String encodedSound = ((JSONObject) response.opt("sound")).optString("data");
                byte[] data = Base64.decode(encodedSound, Base64.DEFAULT);

                File photo=new File(RECEIVE_FILENAME);

                if (photo.exists()) {
                    photo.delete();
                }

                try {
                    FileOutputStream fos=new FileOutputStream(RECEIVE_FILENAME);

                    fos.write(data);
                    fos.flush();
                    fos.close();
                }
                catch (java.io.IOException e) {
                    Log.e("DingDong", "Exception", e);
                }
                onGetRandomSoundComplete.succeeded();
            }

            @Override
            public void onFailure(Throwable e, JSONObject errorResponse) {
                super.onFailure(e, errorResponse);
            }

            @Override
            protected void handleFailureMessage(Throwable e, String responseBody) {
                // TODO - Overridden method to be implemented
                super.handleFailureMessage(e, responseBody);
            }
        });
    }



    public interface OnUploadComplete {
        void succeeded();
        void failed(Throwable e, JSONObject response);
        void failed(Throwable e, String response);
    }

    public interface OnGetRandomSoundComplete {
        void succeeded();
        void failed(Throwable e, JSONObject response);
    }
}
